package com.ruoyi.user.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.user.domain.UserDataStatistics;
import com.ruoyi.user.mapper.UserDataStatisticsMapper;
import com.ruoyi.user.service.IUserDataStatisticsService;

import cn.hutool.core.date.DateUtil;

/**
 * 用户数据统计Service业务层处理
 * 
 * @author DEREK
 * @date 2024-02-19
 */
@Service
public class UserDataStatisticsServiceImpl extends ServiceImpl<UserDataStatisticsMapper, UserDataStatistics> implements IUserDataStatisticsService 
{
    @Autowired
    private UserDataStatisticsMapper userDataStatisticsMapper;

    /**
     * 查询用户数据统计
     * 
     * @param id 用户数据统计主键
     * @return 用户数据统计
     */
    @Override
    public UserDataStatistics selectUserDataStatisticsById(Long id)
    {
        return userDataStatisticsMapper.selectUserDataStatisticsById(id);
    }

    /**
     * 查询用户数据统计列表
     * 
     * @param userDataStatistics 用户数据统计
     * @return 用户数据统计
     */
    @Override
    public List<UserDataStatistics> selectUserDataStatisticsList(UserDataStatistics userDataStatistics)
    {
        return userDataStatisticsMapper.selectUserDataStatisticsList(userDataStatistics);
    }

    /**
     * 新增用户数据统计
     * 
     * @param userDataStatistics 用户数据统计
     * @return 结果
     */
    @Override
    public int insertUserDataStatistics(UserDataStatistics userDataStatistics)
    {
        userDataStatistics.setCreateTime(DateUtils.getNowDate());
        return userDataStatisticsMapper.insertUserDataStatistics(userDataStatistics);
    }

    /**
     * 修改用户数据统计
     * 
     * @param userDataStatistics 用户数据统计
     * @return 结果
     */
    @Override
    public int updateUserDataStatistics(UserDataStatistics userDataStatistics)
    {
        return userDataStatisticsMapper.updateUserDataStatistics(userDataStatistics);
    }

    /**
     * 批量删除用户数据统计
     * 
     * @param ids 需要删除的用户数据统计主键
     * @return 结果
     */
    @Override
    public int deleteUserDataStatisticsByIds(Long[] ids)
    {
        return userDataStatisticsMapper.deleteUserDataStatisticsByIds(ids);
    }

    /**
     * 删除用户数据统计信息
     * 
     * @param id 用户数据统计主键
     * @return 结果
     */
    @Override
    public int deleteUserDataStatisticsById(Long id)
    {
        return userDataStatisticsMapper.deleteUserDataStatisticsById(id);
    }

	@Override
	public void addStatistics(Integer activeNum, Integer addNum) {
		String now = DateUtil.format(new Date(), "yyyyMMdd");
		UserDataStatistics data = this.getOne(new LambdaQueryWrapper<UserDataStatistics>().eq(UserDataStatistics::getStatisticsDay, now));
		if(null == data) {
			data = new UserDataStatistics();
			data.setActiveNum(activeNum);
			data.setAddNum(addNum);
			data.setStatisticsDay(now);
			data.setStatisticsMonth(DateUtil.format(new Date(), "yyyyMM"));
			data.setStatisticsYear(DateUtil.format(new Date(), "yyyy"));
			this.save(data);
		}else {
			data.setActiveNum(data.getActiveNum()+activeNum);
			data.setAddNum(data.getAddNum()+addNum);
			this.updateById(data);
		}
	}
}
